//
//  DictionaryLabel.m
//  LineByLine
//
//  Created by 병철 김 on 11. 12. 16..
//  Copyright (c) 2011 __MyCompanyName__. All rights reserved.
//

#import "DictionaryLabel.h"

#define kFontSize 12


@implementation DictionaryLabel
@synthesize searchWord, meaning;

- (id)init {
    self = [super init];
    if (self) {
        textView = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 1000, 25)];
        [textView setFont:[UIFont systemFontOfSize:kFontSize]];
        
        scrollView = [[UIScrollView alloc] initWithFrame:CGRectZero];
        [scrollView setBackgroundColor:[UIColor whiteColor]];
        [scrollView addSubview:textView];
        [scrollView setShowsVerticalScrollIndicator:NO];
        [scrollView setShowsHorizontalScrollIndicator:NO];
        
        [self addSubview:scrollView];
        [textView release];
        [scrollView release];
    }
    return self;
}

- (void)dealloc {
    [searchWord release];
    [meaning release];
    [super dealloc];
}

- (BOOL) isEqualSearchStringIgnoreCase:(NSString*) targetWord {
    return [[searchWord lowercaseString] isEqualToString:targetWord];
}

- (void) setMeaning:(NSString *)_meaning {
    if (meaning) {
        [meaning release];
    }
    meaning = [_meaning stringByReplacingOccurrencesOfString:@"<b>" withString:@""];
    meaning = [meaning stringByReplacingOccurrencesOfString:@"</b>" withString:@""];
    [meaning retain];
    [self configView];
}

- (void)configView {
    CGRect frame = self.bounds;
    frame.size.height = frame.size.height - 6;
    [scrollView setFrame:frame];
    int width = MAX(frame.size.width, [meaning sizeWithFont:[UIFont systemFontOfSize:kFontSize]].width + 5);
    frame.size.width = width;
    [scrollView setContentSize:frame.size];
    [textView setText:meaning];
}

@end
